package com.frikinet.gestorepub.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

import com.frikinet.gestorepub.web.spring.interceptors.UserLoginInterceptor;
import com.frikinet.gestorepub.web.spring.services.PersistenceService;

@Configuration
@EnableWebMvc
@ComponentScan("com.frikinet.gestorepub.web.spring")
public class DispatcherConfig extends WebMvcConfigurerAdapter
{
	@Autowired
	private PersistenceService persistenceService;
	
	@Bean
	public UrlBasedViewResolver viewResolver()
	{
		UrlBasedViewResolver resolver = new UrlBasedViewResolver();
		resolver.setViewClass(JstlView.class);
		resolver.setPrefix("/WEB-INF/jsp/");
		resolver.setSuffix(".jsp");
		return resolver;
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry)
	{
		InterceptorRegistration userLoginInterceptor = registry.addInterceptor(new UserLoginInterceptor(persistenceService));
		userLoginInterceptor.addPathPatterns("/**");
	}
}
